package com.liveHere.www.dao;

import com.liveHere.www.Exception.OrderDaoException;
import com.liveHere.www.po.*;
import com.liveHere.www.util.JDBCUtils;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;

/**
 * @author Mo312
 */
public class OrderDaoImpl implements OrderDao {

    //获取历史订单
    @Override
    public ArrayList<Order> getOrderList(User user) throws Exception {
        String sql = "select * from orders where user_id = ?";
        ArrayList<Order> orders = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getUserid());
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Order order = new Order();
                order.setShopId(rs.getInt("shop_id"));
                order.setShopName(rs.getString("shop_name"));
                order.setRoomName(rs.getString("room_name"));
                order.setUserId(rs.getInt("user_id"));
                order.setOrderId(rs.getInt("order_id"));
                order.setUsername(rs.getString("username"));
                order.setRoomType(rs.getString("roomType"));
                order.setCheckInDate(rs.getDate("checkInDate").toLocalDate());
                order.setCheckOutDate(rs.getDate("checkOutDate").toLocalDate());
                order.setPrice(rs.getDouble("price"));
                order.setStatus(rs.getString("status"));
                order.setRoomId(rs.getInt("room_id"));
                orders.add(order);
            }
        } catch (SQLException e) {
            throw new OrderDaoException("获取订单列表失败: " + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return orders;
    }

    // 添加订单
    @Override
    public boolean addOrder(Order order) throws SQLException {
        String sql = "INSERT INTO orders (order_id,user_id,shop_id,username,shop_name,room_name,roomType,checkInDate,checkOutDate,price, status,room_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, order.getOrderId());
            pstmt.setInt(2, order.getUserId());
            pstmt.setInt(3, order.getShopId());
            pstmt.setString(4, order.getUsername());
            pstmt.setString(5, order.getShopName());
            pstmt.setString(6, order.getRoomName());
            pstmt.setString(7, order.getRoomType());
            pstmt.setDate(8, java.sql.Date.valueOf(order.getCheckInDate()));
            pstmt.setDate(9, java.sql.Date.valueOf(order.getCheckOutDate()));
            pstmt.setDouble(10, order.getPrice());
            pstmt.setString(11, order.getStatus());
            pstmt.setInt(12, order.getRoomId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new OrderDaoException("添加订单失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    // 更新订单状态
    @Override
    public boolean updateOrderStatus(int orderId, String status) throws SQLException {
        String sql = "UPDATE orders SET status = ? WHERE order_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, status);
            pstmt.setInt(2, orderId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new OrderDaoException("更新订单状态失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    //更新订单退房日期
    @Override
    public boolean updateOrderCheckOutDate(int orderId, LocalDate date) throws SQLException {
        String sql = "UPDATE orders SET checkOutDate = ? WHERE order_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setDate(1, java.sql.Date.valueOf(date));
            pstmt.setInt(2, orderId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new OrderDaoException("更新订单退房日期失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, null);
        }
    }

    //通过订单ID查询订单
    @Override
    public Order getOrderById(int orderId) throws SQLException {
        String sql = "SELECT * FROM orders WHERE order_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, orderId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setShopId(rs.getInt("shop_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setUsername(rs.getString("username"));
                order.setShopName(rs.getString("shop_name"));
                order.setRoomName(rs.getString("room_name"));
                order.setRoomType(rs.getString("roomType"));
                order.setCheckInDate(rs.getDate("checkInDate").toLocalDate());
                order.setCheckOutDate(rs.getDate("checkOutDate").toLocalDate());
                order.setPrice(rs.getDouble("price"));
                order.setStatus(rs.getString("status"));
                order.setRoomId(rs.getInt("room_id"));
                return order;
            }
        } catch (SQLException e) {
            throw new OrderDaoException("查询订单失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return null;
    }

    //根据商铺Id查询订单列表
    @Override
    public ArrayList<Order> getOrderListByShopId(int shopId) throws SQLException {
        String sql = "SELECT * FROM orders WHERE shop_id = ?";
        ArrayList<Order> orders = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, shopId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setShopId(rs.getInt("shop_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setUsername(rs.getString("username"));
                order.setShopName(rs.getString("shop_name"));
                order.setRoomName(rs.getString("room_name"));
                order.setRoomType(rs.getString("roomType"));
                order.setCheckInDate(rs.getDate("checkInDate").toLocalDate());
                order.setCheckOutDate(rs.getDate("checkOutDate").toLocalDate());
                order.setPrice(rs.getDouble("price"));
                order.setStatus(rs.getString("status"));
                order.setRoomId(rs.getInt("room_id"));
                orders.add(order);
            }
        } catch (SQLException e) {
            throw new OrderDaoException("查询订单列表失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return orders;
    }


    //通过商铺ID和订单状态查询
    @Override
    public ArrayList<Order> getOrdersByStatusAndShopId(String status, int shopId) throws SQLException {
        String sql = "SELECT * FROM orders WHERE status = ? AND shop_id = ?";
        ArrayList<Order> orders = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, status);
            pstmt.setInt(2, shopId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setUserId(rs.getInt("user_id"));
                order.setShopId(rs.getInt("shop_id"));
                order.setUsername(rs.getString("username"));
                order.setShopName(rs.getString("shop_name"));
                order.setRoomName(rs.getString("room_name"));
                order.setRoomType(rs.getString("roomType"));
                order.setCheckInDate(rs.getDate("checkInDate").toLocalDate());
                order.setCheckOutDate(rs.getDate("checkOutDate").toLocalDate());
                order.setPrice(rs.getDouble("price"));
                order.setStatus(rs.getString("status"));
                order.setRoomId(rs.getInt("room_id"));
                orders.add(order);
            }
        } catch (SQLException e) {
            throw new OrderDaoException("查询订单列表失败：" + e.getMessage(), e);
        } finally {
            JDBCUtils.close(conn, pstmt, rs);
        }
        return orders;
    }
}
